﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using My.Common.Configuration;
using My.Common.Helper.Utils;
using NCLCustomerManagement.Common.Exception;
using NCLCustomerManagement.DataAccess.User;
using NCLCustomerManagement.DataTransferObjects.User;

namespace NCLCustomerManagement.BusinessLogic.User
{
    public class UserBC : IUserBC
    {
        private IUserRepository _userRepository;

        public UserBC(IUserRepository userRepository)
        {
            _userRepository = userRepository;
        }

        public bool Authenticate(string username, string password)
        {
            password = Hash.CalculateMD5Hash(password);
            return _userRepository.Authenticate(username, password);
        }

        public bool Authenticate(string username, string password, out bool isAdmin, out string manager)
        {
            DataAccess.UserProfile userInfo;
            password = Hash.CalculateMD5Hash(password);
            var auth = _userRepository.Authenticate(username, password, out userInfo);
            if (auth)
            {
                isAdmin = userInfo.IsAdmin;
                manager = userInfo.ReportTo;
            }
            else
            {
                isAdmin = false;
                manager = "";
            }
            return auth;
        }


        public IEnumerable<UserProfileDTO> GetUserListOf(string manager)
        {
            var userList = _userRepository.GetUserListOf(manager);
            return userList;
        }

        public UserProfileDTO GetUserByUsername(string username)
        {
            return _userRepository.GetUserByUsername(username);
        }

        public void AddNewUser(UserProfileDTO userDto)
        {
            try
            {
                var appConfig = new AppConfigurationManager();
                var fromEmailAddress = appConfig.GetAppSetting("FromEmailAddress");
                var fromPassword = appConfig.GetAppSetting("FromEmailPassword");
                var subject = "Thông báo từ Samnamlinhchi Application - Tài khoản đã được tạo";

                var initialPassword = userDto.Password;
                
                var body = string.Format("Tài khoản của bạn đã được tạo: Tên truy cập: <b>{1}</b> - Mật khẩu:<b>{0}</b>. Bạn có thể đổi mật khẩu sau khi đăng nhập", 
                    initialPassword, userDto.Username);
                EmailSender.SendMail(fromEmailAddress, fromPassword, userDto.Email, subject, body);
            }
            catch (Exception)
            {
                throw new CannotSendMailException("Không thể gửi email cho User");
            }

            try
            {
                userDto.Password = Hash.CalculateMD5Hash(userDto.Password);
                _userRepository.AddNewUser(userDto);
            }
            catch (Exception)
            {
                throw new Exception("Không thể thêm User");
            }
            
            
        }

        public void DeleteUser(string username)
        {
            _userRepository.DeleteUser(username);
        }

        public void ActivateUser(string username)
        {
            ActivateOrDeactivateUser(username, true);
        }

        public void DeactivateUser(string username)
        {
            ActivateOrDeactivateUser(username, false);
        }
       
        private void ActivateOrDeactivateUser(string username, bool activate)
        {
            _userRepository.ActivateDeactivateUser(username, activate);
        }


        public void ChangePassword(string username, string newPassword)
        {
            newPassword = Hash.CalculateMD5Hash(newPassword);
            _userRepository.ChangePassword(username, newPassword);
        }
    }
}
